﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Tour.BLL;
using Tour.DAL;

public partial class Modules_Tour_TourTypeAE : VST.Controls.BaseLCGPage
{
    public int? TourTypeId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        TourTypeId = CommonHelper.QueryStringInt("TourTypeId", null);
        if (!IsPostBack)
        {
            LoadDataDLL();
            LoadData();

        }
    }
    void LoadData()
    {
        if (TourTypeId.HasValue)
        {
            TblTourTypeEntity item = TblTourTypeManager.CreateInstant().SelectOne(TourTypeId.Value);
            txtName.Text = item.Name;
            //txtUrl.Text = item.Url;
            txtTitle.Text = item.Title;
            txtKeywords.Text = item.Keywords;
            txtDescription.Text = item.Description;
            ddlParent.SelectedValue = item.ParentId.ToString();
            chkVisible.Checked = item.IsVisible;
        }
    }
    void LoadDataDLL()
    {
        ddlParent.DataSource = TblTourTypeManager.CreateInstant().SelectTblTourTypeForDDL(-1, WorkingLanguage);
        ddlParent.DataTextField = "Name";
        ddlParent.DataValueField = "Id";
        ddlParent.DataBind();

        //<asp:ListItem Value="-1">------ACBS---- </asp:ListItem>
    }
    TblTourTypeEntity GetTblTourType()
    {
        TblTourTypeEntity item = new TblTourTypeEntity();
        if (TourTypeId.HasValue)
        {
            item = TblTourTypeManager.CreateInstant().SelectOne(TourTypeId.Value);
            if (ddlParent.SelectedValue.ToInt() != item.ParentId)
            {
                UpdateOrderOffAndDelete(item.Id.ToString(), item.ParentId.ToString(), 100);
            }
            item.UpdatedBy = User.Identity.Name;
            item.UpdatedDate = DateTime.Now;
        }
        else
        {
            item.TextId = TblTourTypeManager.CreateInstant().GetUniqueTextIdFromUnicodeText(txtName.Text);
            item.DisplayOrder = 1;
            item.CreatedBy = User.Identity.Name;
            item.CreatedDate = DateTime.Now;
            item.UpdatedBy = User.Identity.Name;
            item.UpdatedDate = DateTime.Now;
        }
        item.LanguageId = WorkingLanguage;
        item.IsVisible = chkVisible.Checked;
        item.Name = txtName.Text;
        item.ParentId = ddlParent.SelectedValue.ToInt();
        item.Title = txtTitle.Text;
        item.Description = txtDescription.Text;
        item.Keywords = txtKeywords.Text;
        return item;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        TblTourTypeManager.CreateInstant().InsertUpdate(GetTblTourType());
        TblTourTypeManager.CreateInstant().SortTblTourType(int.Parse(ddlParent.SelectedValue.ToString()));
        Response.Redirect("TourTypeList.aspx");
    }
    private void UpdateOrderOffAndDelete(string strId, string strParentId, int NewIndex)
    {
        string Whereclause = " And ParentId IS NULL ";
        if (!strParentId.Equals(""))
        {
            Whereclause = String.Format(" And ParentId={0}", strParentId);
        }
        string Whereclause1 = String.Format(" ID={0}", strId);
        string Whereclause2 = String.Format(" And Id<>{0}", strId);
        Tools.SqlHelper.UpdateOrders("TblTourType", Whereclause, Whereclause1, Whereclause2, NewIndex);
    }
}